using B.S.Medical.Api.Read.Application.Command.Department;
using B.S.Medical.Api.Read.DTOS;
using B.S.Medical.Domain.Department;
using B.S.Medical.Domain.Doctor;
using B.S.Medical.ErrorCode;
using B.S.Medical.Infrastructure.Interface;
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Medical.Api.Read.Application.Handler.Department
{
    /// <summary>
    /// 根据医生ID获取科室信息处理器
    /// </summary>
    public class GetDepartmentByDoctorIdHandler : IRequestHandler<GetDepartmentByDoctorIdCommand, ApiResult<ManageModelDTO>>
    {
        private readonly IBaseRepository<ManageModel> _departmentRepository;
        private readonly IBaseRepository<DoctorModel> _doctorRepository;
        private readonly IBaseRepository<CampusModel> _campusRepository;
        private readonly ILogger<GetDepartmentByDoctorIdHandler> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        public GetDepartmentByDoctorIdHandler(
            IBaseRepository<ManageModel> departmentRepository,
            IBaseRepository<DoctorModel> doctorRepository,
            IBaseRepository<CampusModel> campusRepository,
            ILogger<GetDepartmentByDoctorIdHandler> logger)
        {
            _departmentRepository = departmentRepository;
            _doctorRepository = doctorRepository;
            _campusRepository = campusRepository;
            _logger = logger;
        }

        /// <summary>
        /// 处理根据医生ID获取科室信息请求
        /// </summary>
        public async Task<ApiResult<ManageModelDTO>> Handle(GetDepartmentByDoctorIdCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<ManageModelDTO>();

            try
            {
                // 查询医生信息
                var doctor = await _doctorRepository.GetValue(request.DoctorId);
                if (doctor == null || doctor.IsDeleted)
                {
                    result.Code = ApiEnum.Fail;
                    result.Msg = "未找到指定ID的医生信息";
                    return result;
                }

                // 根据医生的科室ID查询科室信息
                var department = await _departmentRepository.GetValue(doctor.DepartmentId);
                if (department == null || department.IsDeleted)
                {
                    result.Code = ApiEnum.Fail;
                    result.Msg = "未找到该医生所属的科室信息";
                    return result;
                }

                // 查询院区信息
                var campus = await _campusRepository.GetValue(department.CampusId);
                string campusName = campus != null && !campus.IsDeleted ? campus.CampusName : "";

                // 转换为DTO
                var departmentDto = new ManageModelDTO
                {
                    Id = department.Id,
                    DepartmentName = department.DepartmentName,
                    DepartmentAddr = department.DepartmentAddr,
                    CampusId = department.CampusId,
                    CampusName = campusName,
                    ParetId = department.ParetId,
                    Status = department.Status
                };

                result.Code = ApiEnum.Success;
                result.Msg = "获取科室信息成功";
                result.Data = departmentDto;

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取科室信息时发生错误，医生ID: {request.DoctorId}");
                result.Code = ApiEnum.Error;
                result.Msg = "获取科室信息失败：" + ex.Message;
                return result;
            }
        }
    }
} 